<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.fileupload.FileUpload" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>

<%@ page import="cn.entity.News1" %>
<%@ page import="cn.NewsDao.NewsDao1" %>
<%@ page import="cn.NewsDaoImpl.NewsDaoImpl1" %><%--
  Created by IntelliJ IDEA.
  User: 31052
  Date: 2022/4/7
  Time: 10:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
        <%
            News1 news = (News1) session.getAttribute("news");
            //文件上传
            boolean multipartContent = ServletFileUpload.isMultipartContent(request);
            //地址
            String path = "D:\\untitled\\web\\img";
            File file = new File(path);
            //判断目录是否存在
            if (!file.exists()){
                file.mkdir();
            }
            //判断文件表单提交是否正确
            if (multipartContent){
                //创建文件上传的核心工厂
                FileItemFactory fileItemFactory = new DiskFileItemFactory();
                //文件上传对象
                ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
                //获取表单的文件对象
                List<FileItem> list = servletFileUpload.parseRequest(request);
                //上传数据
                Iterator<FileItem> item = list.iterator();
                String filedTitle = null;
                String picPath = null;
                while (item.hasNext()){
                    FileItem fileItem = item.next();
                    boolean formField = fileItem.isFormField();
                    if (formField){
                        String fieldName = fileItem.getFieldName();
                        if (fieldName.equals("picPath1")){
                            filedTitle = fileItem.getString("utf-8");
                        }
                    }else {
                        //文件名称
                        String name = fileItem.getName();
                        File file1 = new File(name);
                        if (file1.getName() != null && !(file1.getName().equals(""))){
                            //将文件上传指定的值
                            File file2 = new File(path,file1.getName());
                            //完成文件写入
                            fileItem.write(file2);
                            picPath = file2.getName();
                        }

                    }
                }

                news.setPicPath(picPath);
                NewsDao1 newsDao1 = new NewsDaoImpl1();
                if ( newsDao1.update(news)){
                    response.sendRedirect(request.getContextPath()+"/index4.jsp");
                }else {
        %>
                    <div>修改失败</div>
        <%
                }
            }
        %>
</body>
</html>
